local evl_Minimap = CreateFrame("Frame", nil, Minimap)

local loc
local text
local lastUpdate = 0

function evl_Minimap:new()
	self:EnableMouse(false)
	self:SetPoint("TOPLEFT", Minimap, "TOPLEFT")
	self:SetPoint("BOTTOMRIGHT", Minimap, "BOTTOMRIGHT")
	self:EnableMouseWheel(true)
	self:SetScript("OnMouseWheel", function() self:Zoom() end)
	self:SetScript("OnUpdate", self.onUpdate)

	loc = CreateFrame("Button", nil, Minimap)
	loc:SetWidth(60)
	loc:SetHeight(16)
	loc:SetPoint("BOTTOM", self, "BOTTOM", 0, 5)
	loc:SetScript("OnEnter", function() self:ShowToolTip() end)
	loc:SetScript("OnLeave", function() GameTooltip:Hide() end)
	
	text = loc:CreateFontString(nil, "OVERLAY")
	text:SetWidth(60)
	text:SetHeight(16)
	text:SetPoint("TOP", loc)
	text:SetJustifyH("CENTER")
	text:SetFontObject(GameFontNormalSmall)

	MinimapZoomIn:Hide()
	MinimapZoomOut:Hide()
	GameTimeFrame:Hide()
	MiniMapWorldMapButton:Hide()
	MinimapToggleButton:Hide()
	MinimapZoneTextButton:Hide()
	MinimapBorderTop:Hide()
	MinimapNorthTag:Hide()

	-- Tracking
	--[[
	MiniMapTrackingBorder:Hide()
	MiniMapTrackingBackground:Hide()
	MiniMapTracking:SetParent(Minimap)
	MiniMapTracking:ClearAllPoints()
	MiniMapTracking:SetPoint("TOPLEFT")
	--MiniMapTracking:Hide()
	]]

	-- Battlefield
	--[[
	MiniMapBattlefieldBorder:Hide()
	MiniMapBattlefieldFrame:SetParent(Minimap)
	MiniMapBattlefieldFrame:ClearAllPoints()
	MiniMapBattlefieldFrame:SetPoint("TOPRIGHT", 0, -3)
	]]

	-- Mail
	--[[
	MiniMapMailBorder:Hide()
	MiniMapMailFrame:SetParent(Minimap)
	MiniMapMailFrame:ClearAllPoints()
	MiniMapMailFrame:SetPoint("TOP")
	MiniMapMailIcon:SetTexture("Interface\\AddOns\\evl_Art\\mail")
	]]
end

function evl_Minimap:Zoom()
    if not arg1 then return end
    if arg1 > 0 and Minimap:GetZoom() < 5 then
        Minimap:SetZoom(Minimap:GetZoom() + 1)
    elseif arg1 < 0 and Minimap:GetZoom() > 0 then
        Minimap:SetZoom(Minimap:GetZoom() - 1)
    end
end

function evl_Minimap:onUpdate(elapsed)
	lastUpdate = lastUpdate + elapsed
	
	if lastUpdate > 1 then
		lastUpdate = 0
    x, y = GetPlayerMapPosition("player")
    text:SetText(string.format("%s,%s", floor(x * 100) or "", floor(y * 100) or ""))
  end
end

function evl_Minimap:ShowToolTip()
	local pvpType, isSubZonePvP, factionName = GetZonePVPInfo()
	local zoneName = GetZoneText()
	local subzoneName = GetSubZoneText()

	if subzoneName == zoneName then
		subzoneName = ""
	end
	
	GameTooltip:SetOwner(loc, "ANCHOR_BOTTOMRIGHT", -100)
	GameTooltip:AddLine(zoneName, 1.0, 1.0, 1.0)

	if pvpType == "sanctuary" then
		GameTooltip:AddLine(subzoneName, 0.41, 0.8, 0.94)
		GameTooltip:AddLine(SANCTUARY_TERRITORY, 0.41, 0.8, 0.94)
	elseif pvpType == "arena" then
		GameTooltip:AddLine(subzoneName, 1.0, 0.1, 0.1)
		GameTooltip:AddLine(FREE_FOR_ALL_TERRITORY, 1.0, 0.1, 0.1)
	elseif pvpType == "friendly" then
		GameTooltip:AddLine(subzoneName, 0.1, 1.0, 0.1)
		GameTooltip:AddLine(format(FACTION_CONTROLLED_TERRITORY, factionName), 0.1, 1.0, 0.1)
	elseif pvpType == "hostile" then
		GameTooltip:AddLine(subzoneName, 1.0, 0.1, 0.1)
		GameTooltip:AddLine(format(FACTION_CONTROLLED_TERRITORY, factionName), 1.0, 0.1, 0.1)
	elseif pvpType == "contested" then
		GameTooltip:AddLine(subzoneName, 1.0, 0.7, 0.0)
		GameTooltip:AddLine(CONTESTED_TERRITORY, 1.0, 0.7, 0.0)
	else
		GameTooltip:AddLine(subzoneName, NORMAL_FONT_COLOR.r, NORMAL_FONT_COLOR.g, NORMAL_FONT_COLOR.b)
	end

	GameTooltip:Show()
end

evl_Minimap:new()